package com.authine.cloudpivot.ext.kingdee.jdbc;

import com.alibaba.cola.dto.RequestContext;

import com.alibaba.fastjson.JSON;
import com.authine.cloudpivot.ext.constants.AppConst;
import com.authine.cloudpivot.ext.constants.KingDeeConstant;
import com.authine.cloudpivot.ext.constants.ModelConst;
import com.authine.cloudpivot.ext.dto.voucher.KingDeeBasicStats;
import com.authine.cloudpivot.ext.dto.voucher.RecordVoucher;
import com.authine.cloudpivot.ext.enums.kingdee.BusinessType;
import com.authine.cloudpivot.ext.utils.BoResultFormat;
import com.authine.cloudpivot.ext.utils.JdbcTemplateUtils;
import com.authine.mvp.app.launcher.utils.JsonParseObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;


import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @Author: liwei
 * @DateTime: 2021/7/19
 * @Description: TODO
 */
@Slf4j
public class RecordVoucherRepository {

    /**
     *  新建凭证记录数据
     * @param data
     * @return
     */
    public int create(Map<String, Object> data) {
        BoResultFormat.initSys(RequestContext.getCorpLoginId(), data);
        return JdbcTemplateUtils.builder(AppConst.FINANCIALCENTER, ModelConst.T_RECORD_VOUCHER).putFieldAndVal(data).insert();
    }

    /**
     * 根据id 删除凭证记录数据
     * @param id
     * @return
     */
    public int del(String id) {
        return JdbcTemplateUtils.builder(AppConst.FINANCIALCENTER, ModelConst.T_RECORD_VOUCHER).putFieldAndVal(KingDeeConstant.DELETED_FIELD, 1).eq(KingDeeConstant.ID_FIELD, id).update();
    }

    /**
     * 根据 业务id 查询匹配数据列表
     * @param businessId
     * @return
     */
    public List<RecordVoucher> findByBusinessId(String businessId) {

        List<Map<String, Object>> mapList = JdbcTemplateUtils.builder(AppConst.FINANCIALCENTER, ModelConst.T_RECORD_VOUCHER)
                .selectField(" * ")
                .eq(KingDeeConstant.BUSINESS_ID_FIELD, businessId)
                .queryForList();

        if(CollectionUtils.isEmpty(mapList)){
            return null;
        }

        List<RecordVoucher> recordVoucherList = JsonParseObject.parseObject(JSON.toJSONString(mapList), RecordVoucher.class);
        return recordVoucherList;
    }


    /**
     * 根据业务id & 业务类型 查询匹配数据
     * @param id
     * @param businessType
     * @return
     */
    public List<RecordVoucher> findByBusinessIdAndBusinessType(String id, BusinessType businessType) {
        //根据参数查询 记录数据
        List<Map<String, Object>> mapList = JdbcTemplateUtils.builder(AppConst.FINANCIALCENTER, ModelConst.T_RECORD_VOUCHER).eq(KingDeeConstant.BUSINESS_ID_FIELD, id)
                .selectField(" * ")
                .eq(KingDeeConstant.BUSINESS_TYPE_FIELD, businessType.name())
                .eq(KingDeeConstant.DELETED_FIELD, 0)
                .eq(KingDeeConstant.IS_SUCCESS_FIELD, true).queryForList();

        if(CollectionUtils.isEmpty(mapList)){
            return null;
        }
        List<RecordVoucher> recordVoucherList = JsonParseObject.parseObject(JSON.toJSONString(mapList), RecordVoucher.class);
        return recordVoucherList;
    }

    public List<RecordVoucher> findByBusinessIds(Collection<String> businessIds) {

        List<Map<String, Object>> mapList = JdbcTemplateUtils.builder(AppConst.FINANCIALCENTER, ModelConst.T_RECORD_VOUCHER)
                .selectField(" * ")
                .in(KingDeeConstant.BUSINESS_ID_FIELD, businessIds).queryForList();

        if(CollectionUtils.isEmpty(mapList)){
            return null;
        }
        List<RecordVoucher> recordVoucherList = JsonParseObject.parseObject(JSON.toJSONString(mapList), RecordVoucher.class);
        return recordVoucherList;
    }
}
